package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.mapper.ProcessMapper;
import com.youlai.boot.factory.model.entity.Process;
import com.youlai.boot.factory.service.ProcessTemplateDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.ProcessTemplateDetailForm;
import com.youlai.boot.factory.model.query.ProcessTemplateDetailQuery;
import com.youlai.boot.factory.model.vo.ProcessTemplateDetailVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 工序模板明细前端控制层
 *
 * @author youlaitech
 * @since 2025-10-01 05:19
 */
@Tag(name = "工序模板明细接口")
@RestController
@RequestMapping("/api/v1/process-template-detail")
@RequiredArgsConstructor
public class ProcessTemplateDetailController  {

    private final ProcessTemplateDetailService processTemplateDetailService;

    @Operation(summary = "工序模板明细分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:process-template-detail:query')")
    public PageResult<ProcessTemplateDetailVO> getProcessTemplateDetailPage(ProcessTemplateDetailQuery queryParams ) {
        IPage<ProcessTemplateDetailVO> result = processTemplateDetailService.getProcessTemplateDetailPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "工序模板连工序表明细分页列表")
    @GetMapping("/joinPage")
    @PreAuthorize("@ss.hasPerm('factory:process-template-detail:query')")
    public PageResult<ProcessTemplateDetailVO> getProcessTemplateDetailJoinPage(ProcessTemplateDetailQuery queryParams ) {
        IPage<ProcessTemplateDetailVO> result = processTemplateDetailService.getProcessTemplateDetailJoinPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增工序模板明细")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:process-template-detail:add')")
    public Result<Void> saveProcessTemplateDetail(@RequestBody @Valid ProcessTemplateDetailForm formData ) {
        boolean result = processTemplateDetailService.saveProcessTemplateDetail(formData);
        return Result.judge(result);
    }

    @Autowired
    private ProcessMapper processMapper;

    @Operation(summary = "获取工序模板明细表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:process-template-detail:edit')")
    public Result<ProcessTemplateDetailForm> getProcessTemplateDetailForm(
        @Parameter(description = "工序模板明细ID") @PathVariable Long id
    ) {
        ProcessTemplateDetailForm formData = processTemplateDetailService.getProcessTemplateDetailFormData(id);
        //查询工序表
        Process process = processMapper.selectById(formData.getProcessId());
        formData.setProcessName(process.getProcessName());
        return Result.success(formData);
    }

    @Operation(summary = "修改工序模板明细")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:process-template-detail:edit')")
    public Result<Void> updateProcessTemplateDetail(
            @Parameter(description = "工序模板明细ID") @PathVariable Long id,
            @RequestBody @Validated ProcessTemplateDetailForm formData
    ) {
        boolean result = processTemplateDetailService.updateProcessTemplateDetail(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除工序模板明细")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:process-template-detail:delete')")
    public Result<Void> deleteProcessTemplateDetails(
        @Parameter(description = "工序模板明细ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = processTemplateDetailService.deleteProcessTemplateDetails(ids);
        return Result.judge(result);
    }
}
